package pers.echo.tester.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import pers.echo.tester.entity.TblBulletins;
import pers.echo.tester.entity.TblCkqinfo;
import pers.echo.tester.entity.TblLandinfo;
import pers.echo.tester.entity.TblTkqinfo;
import pers.echo.tester.enums.ResTypeEnum;
import pers.echo.tester.mapper.TblBulletinsMapper;
import pers.echo.tester.mapper.TblCkqinfoMapper;
import pers.echo.tester.mapper.TblLandinfoMapper;
import pers.echo.tester.mapper.TblTkqinfoMapper;
import pers.echo.tester.service.ITblBulletinsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * ????? 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-09-17
 */
@Service
public class TblBulletinsServiceImpl extends ServiceImpl<TblBulletinsMapper, TblBulletins> implements ITblBulletinsService {

    @Autowired(required=false)
    TblBulletinsMapper mapper;

    @Autowired(required=false)
    TblLandinfoMapper landinfoMapper;

    @Autowired(required=false)
    TblCkqinfoMapper ckqinfoMapper;

    @Autowired(required=false)
    TblTkqinfoMapper tkqinfoMapper;


    @Override
    public TblBulletins getblBulletinsTemplate(ResTypeEnum resTypeEnum) {

        // ATTENTION
        // F_TYPE, F_NO, F_PUBLISHD, F_XZQDM, F_TRANSTYPE
        // F_JYJGMC, APPGOV, OWNERANME,

        TblBulletins bulletins = mapper.getFirstBulletins(resTypeEnum.value);
        if(bulletins!=null){
            if(resTypeEnum==ResTypeEnum.LAND){
                bulletins.setFWord("测试赣国土资网土地");
            }else{
                bulletins.setFWord("测试赣国土资网交矿");
            }
            bulletins.setBulletinid(null);
            bulletins.setFNo(null);
            LocalDateTime beginTime = LocalDateTime.now();
            LocalDateTime bailTime = beginTime.plusDays(10);
            LocalDateTime startTime = beginTime.plusDays(20);
            LocalDateTime closeTime = beginTime.plusDays(30);

            bulletins.setFPublished("N");

            bulletins.setFBegintime(beginTime);
            bulletins.setFBailtime(bailTime);
            bulletins.setFGbegintime(startTime);
            bulletins.setFGclosetime(closeTime);
            bulletins.setFBapplytime(beginTime);
            bulletins.setCreatedtime(beginTime);
            bulletins.setLastfixtime(beginTime);

            return bulletins;

        }
        return null;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void removeTestBulletinRes() {

        List<TblBulletins> bulletinsList = mapper.selectList(Wrappers.<TblBulletins>lambdaQuery().likeRight(TblBulletins::getFWord, "测试"));

        for(TblBulletins bulletins: bulletinsList){
            String resType = bulletins.getFType();
            if(resType.equals(ResTypeEnum.LAND.value)){
                List<TblLandinfo> landinfoList = landinfoMapper.selectList(Wrappers.<TblLandinfo>lambdaQuery().eq(TblLandinfo::getBulletinid, bulletins.getBulletinid()));
                landinfoMapper.deleteBatchIds(landinfoList);
            }else if(resType.equals(ResTypeEnum.CKQ.value)){
                List<TblCkqinfo> ckqinfoList = ckqinfoMapper.selectList(Wrappers.<TblCkqinfo>lambdaQuery().eq(TblCkqinfo::getBulletinid, bulletins.getBulletinid()));
                ckqinfoMapper.deleteBatchIds(ckqinfoList);
            }else if(resType.equals(ResTypeEnum.TKQ.value)){
                List<TblTkqinfo> tkqinfoList = tkqinfoMapper.selectList(Wrappers.<TblTkqinfo>lambdaQuery().eq(TblTkqinfo::getBulletinid, bulletins.getBulletinid()));
                tkqinfoMapper.deleteBatchIds(tkqinfoList);
            }
        }
    }
}


